package com.stock.dao;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * Stockanlyz entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.stock.dao.Stockanlyz
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class StockanlyzDAO {
	private static final Logger log = LoggerFactory
			.getLogger(StockanlyzDAO.class);
	// property constants
	public static final String INFO = "info";
	public static final String AMOUNT_BAT = "amountBat";
	public static final String AMT_CRE_DAYS = "amtCreDays";
	public static final String MIN_PRICE = "minPrice";
	public static final String MIN_TO_DAYS = "minToDays";
	public static final String MAX_PICE = "maxPice";
	public static final String MAX_TO_DAYS = "maxToDays";
	public static final String UP_DAYS = "upDays";
	public static final String IS_OWN = "isOwn";
	public static final String FALL_DAYS = "fallDays";
	public static final String AVER_AMOUNT = "averAmount";
	public static final String MESSAGE = "message";
	public static final String RATE_PERCENT = "ratePercent";
	public static final String DAY_PERCENT_WAVE = "dayPercentWave";
	public static final String DEAL_LEVEL = "dealLevel";
	public static final String PRICE_LEVEL = "priceLevel";
	public static final String DEALMARK = "dealmark";
	public static final String SCORE = "score";
	public static final String IDEAL_PRICE = "idealPrice";
	public static final String CUR_PRICE = "curPrice";
	public static final String REDTHREE = "redthree";
	public static final String UPTHREE = "upthree";
	public static final String DOWNTHREE = "downthree";
	public static final String THREECROW = "threecrow";
	public static final String VENUS = "venus";
	public static final String HESPERUS = "hesperus";
	public static final String THREECLOUD = "threecloud";
	public static final String CSC = "csc";
	public static final String SCS = "scs";
	public static final String FIVE_DAY_PRICE = "fiveDayPrice";
	public static final String TEN_DAY_PRICE = "tenDayPrice";
	public static final String THIRTY_DAY_PRICE = "thirtyDayPrice";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Stockanlyz transientInstance) {
		log.debug("saving Stockanlyz instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Stockanlyz persistentInstance) {
		log.debug("deleting Stockanlyz instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Stockanlyz findById(java.lang.String id) {
		log.debug("getting Stockanlyz instance with id: " + id);
		try {
			Stockanlyz instance = (Stockanlyz) getCurrentSession().get(
					"com.stock.dao.Stockanlyz", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Stockanlyz instance) {
		log.debug("finding Stockanlyz instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("com.stock.dao.Stockanlyz")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Stockanlyz instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Stockanlyz as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByInfo(Object info) {
		return findByProperty(INFO, info);
	}

	public List findByAmountBat(Object amountBat) {
		return findByProperty(AMOUNT_BAT, amountBat);
	}

	public List findByAmtCreDays(Object amtCreDays) {
		return findByProperty(AMT_CRE_DAYS, amtCreDays);
	}

	public List findByMinPrice(Object minPrice) {
		return findByProperty(MIN_PRICE, minPrice);
	}

	public List findByMinToDays(Object minToDays) {
		return findByProperty(MIN_TO_DAYS, minToDays);
	}

	public List findByMaxPice(Object maxPice) {
		return findByProperty(MAX_PICE, maxPice);
	}

	public List findByMaxToDays(Object maxToDays) {
		return findByProperty(MAX_TO_DAYS, maxToDays);
	}

	public List findByUpDays(Object upDays) {
		return findByProperty(UP_DAYS, upDays);
	}

	public List findByIsOwn(Object isOwn) {
		return findByProperty(IS_OWN, isOwn);
	}

	public List findByFallDays(Object fallDays) {
		return findByProperty(FALL_DAYS, fallDays);
	}

	public List findByAverAmount(Object averAmount) {
		return findByProperty(AVER_AMOUNT, averAmount);
	}

	public List findByMessage(Object message) {
		return findByProperty(MESSAGE, message);
	}

	public List findByRatePercent(Object ratePercent) {
		return findByProperty(RATE_PERCENT, ratePercent);
	}

	public List findByDayPercentWave(Object dayPercentWave) {
		return findByProperty(DAY_PERCENT_WAVE, dayPercentWave);
	}

	public List findByDealLevel(Object dealLevel) {
		return findByProperty(DEAL_LEVEL, dealLevel);
	}

	public List findByPriceLevel(Object priceLevel) {
		return findByProperty(PRICE_LEVEL, priceLevel);
	}

	public List findByDealmark(Object dealmark) {
		return findByProperty(DEALMARK, dealmark);
	}

	public List findByScore(Object score) {
		return findByProperty(SCORE, score);
	}

	public List findByIdealPrice(Object idealPrice) {
		return findByProperty(IDEAL_PRICE, idealPrice);
	}

	public List findByCurPrice(Object curPrice) {
		return findByProperty(CUR_PRICE, curPrice);
	}

	public List findByRedthree(Object redthree) {
		return findByProperty(REDTHREE, redthree);
	}

	public List findByUpthree(Object upthree) {
		return findByProperty(UPTHREE, upthree);
	}

	public List findByDownthree(Object downthree) {
		return findByProperty(DOWNTHREE, downthree);
	}

	public List findByThreecrow(Object threecrow) {
		return findByProperty(THREECROW, threecrow);
	}

	public List findByVenus(Object venus) {
		return findByProperty(VENUS, venus);
	}

	public List findByHesperus(Object hesperus) {
		return findByProperty(HESPERUS, hesperus);
	}

	public List findByThreecloud(Object threecloud) {
		return findByProperty(THREECLOUD, threecloud);
	}

	public List findByCsc(Object csc) {
		return findByProperty(CSC, csc);
	}

	public List findByScs(Object scs) {
		return findByProperty(SCS, scs);
	}

	public List findByFiveDayPrice(Object fiveDayPrice) {
		return findByProperty(FIVE_DAY_PRICE, fiveDayPrice);
	}

	public List findByTenDayPrice(Object tenDayPrice) {
		return findByProperty(TEN_DAY_PRICE, tenDayPrice);
	}

	public List findByThirtyDayPrice(Object thirtyDayPrice) {
		return findByProperty(THIRTY_DAY_PRICE, thirtyDayPrice);
	}

	public List findAll() {
		log.debug("finding all Stockanlyz instances");
		try {
			String queryString = "from Stockanlyz";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Stockanlyz merge(Stockanlyz detachedInstance) {
		log.debug("merging Stockanlyz instance");
		try {
			Stockanlyz result = (Stockanlyz) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Stockanlyz instance) {
		log.debug("attaching dirty Stockanlyz instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Stockanlyz instance) {
		log.debug("attaching clean Stockanlyz instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static StockanlyzDAO getFromApplicationContext(ApplicationContext ctx) {
		return (StockanlyzDAO) ctx.getBean("StockanlyzDAO");
	}
}